package cn.tsmchina.one.service.order;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.tsmchina.one.dao.order.OrderDao;
import cn.tsmchina.one.dao.user.UserDao;
import cn.tsmchina.one.domain.order.Order;
import cn.tsmchina.one.domain.user.User;
import cn.tsmchina.one.pojo.OrderPojo;
import cn.tsmchina.one.pojo.ProductPojo;
import cn.tsmchina.one.util.AESUtil;
import cn.tsmchina.one.util.C;
import cn.tsmchina.one.util.ConfigUtil;
import cn.tsmchina.one.util.JsonUtil;
import cn.tsmchina.one.util.OrderUtil;


@Service
public class OrderServiceImpl implements OrderService 
{

	@Autowired
	private OrderDao orderDao;
	
	@Autowired
	private UserDao userDao;
	
	@Override
	public List<Order> getQueryOrders(Map<String, String> parameter, int page, long customerId)
	{
		List<Order> orders = orderDao.getQueryOrders(parameter, page, customerId);
		return orders;
	}
	@Override
	public int getTotal(Map<String, String> parameter, long customerId)
	{
		return orderDao.getTotal(parameter, customerId);
	}
	@Override
	public Map<String, Object> addOrder(OrderPojo orderPojo)
	{
		Map<String, Object> data = new HashMap<>();
		Order order = new Order();
		order.setUserId(orderPojo.getUserId());
		order.setOrderId(OrderUtil.createOrderId());
		order.setDate(new Date());
		order.setPayStatus(C.PayStatus.no);
		order.setPayType(orderPojo.getPayType());
		order.setSendStatus(C.SendStatus.no);
		order.setTel(orderPojo.getTel());
		order.setToAddress(orderPojo.getAddress());
		order.setCustomerId(orderPojo.getCustomerId());
		order.setMemo(orderPojo.getMemo());
		order.setTel(orderPojo.getTel());
		order.setName(orderPojo.getName());
		order.setAddress(orderPojo.getRedAddress());//addedbyhy 4.18新增字段
		int credits = orderPojo.getUseCredits();
		//验证积分是否有效
		User user = userDao.getUserById(""+orderPojo.getUserId());
		if(credits>user.getCredits()) {
			data.put("type", "error");
			data.put("msg","错误,您没有足够的积分可以使用!");
			return data;
		}
		double realPrice = orderPojo.getTotalPrice() - credits*C.SCORE_CASH_EXCHANGE_RATIO;//TODO 使兑换比例可配置
		DecimalFormat df=new DecimalFormat(".##");
		realPrice = Double.parseDouble(df.format(realPrice));
//		order.setTotalPrice(orderPojo.getTotalPrice());
		order.setTotalPrice(realPrice);
		order.setCredits(orderPojo.getUseCredits());
		order.setPeopleCount(orderPojo.getPeople());
		order.setArriveTime(orderPojo.getArriveTime());
		List<ProductPojo> products = orderPojo.getProducts();
		if(products.size() > 0)
		{
			//添加订单的产品
			String productJson = JsonUtil.parseJson(products);
			order.setProducts(productJson);
			//添加订单
			orderDao.addOrder(order);
			if(order.getPayType() == C.PayType.online)
			{
				data.put("type", "online");
				Map<String, Object>  map = new HashMap<>();
				map.put("orderId",order.getOrderId());
				map.put("price", order.getTotalPrice());
				map.put("name", ConfigUtil.get("orderName"));
				String parameter = AESUtil.encrypt(JsonUtil.parseJson(map));
				String url ="../pay/trade?p="+parameter;
				data.put("url",url);
				/* 与微信支付的参数名一致  */
				data.put("name",  ConfigUtil.get("orderName"));
				data.put("totalPrice", order.getTotalPrice());
				data.put("orderNo", order.getOrderId());
				/*   */
				
			}
			if(order.getPayType() == C.PayType.offline)
			{
				data.put("type", "offline");
				data.put("msg", "下单成功!");
			}
		}
		else
		{
			data.put("type", "error");
			data.put("msg","错误,没有购买产品不能提交订单!");
		}
		return data;
	}
	@Override
	public void updatePayStatus(Long orderId)
	{
		orderDao.updatePayStatus(orderId);
	}
	@Override
	public void updateSendStatus(long orderId, int status)
	{
		orderDao.updateSendStatus(orderId, status);
	}
	
	//获取用户订单
	@Override
	public List<Order> getUserOrders(Long userId, long customerId) {
		if(null != userId){
			return orderDao.getUserOrders(userId, customerId);
		}
		
		return null;
	}
	@Override
	public void deleteOrder(long orderId) {
		orderDao.deleteOrder(orderId);
	}
	@Override
	public List<Order> getUserOrders(Long userId, int page, long customerId) {
		return orderDao.getUserOrders(userId, page, customerId);
	}
	
	@Override
	public List<Order> getOrdersByName(String name) {
		return orderDao.getOrdersByName(name);
	}
	@Override
	public int getTotalNum() {
		return orderDao.getTotalNum();
	}
}
